package com.acme.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.acme.dao.UserDAO;
import com.acme.entity.Claim;
import com.acme.entity.User;

@Service
public class UserDAOImpl implements UserDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public List<User> getAllUsers() {
		Query query = sessionFactory.getCurrentSession().createQuery("from User");
		List<?> l = query.list();
		List<User> userList = new ArrayList<User>();
		for (Object o : l) {
			userList.add((User) o);
		}
		
		return userList;
	}

	@Override
	public User getUserByID(int userID) {
		return (User) sessionFactory.getCurrentSession().get(User.class, userID);
	}

	@Override
	public void updateUser(User user) {
		sessionFactory.getCurrentSession().merge(user);
	}

	@Override
	public void deleteUser(User user) {
		sessionFactory.getCurrentSession().delete(user);
	}

	@Override
	public List<User> getUsersWithRejectedClaims() {
		User insertUser = new User();
		String querystring = "from Claim where status = 'Rejected'";
		Query query = sessionFactory.getCurrentSession().createQuery(querystring);
		List<?> l = query.list(); // query returns a raw list. 
		ArrayList<Claim> claimList = new ArrayList<Claim>();
		for (Object o : l)
			claimList.add((Claim) o); // Cast each query object as a claim
		ArrayList<User> userList = new ArrayList<User>();
		for (Claim claim : claimList) { // for each rejected claim, add the unique user to a userlist
			insertUser = getUserByID(claim.getUser().getUserID());
			if(!userList.contains(insertUser))
				userList.add(insertUser);
		}
		return userList;
	}

	private DataSource dataSource;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	/*--------------------------------------------------------------------------------------*/
	public void insert(User user) {
		String sql = "INSERT INTO USERS3" + "(fName, lName, birthday, idNumber, ssn, userName, password) VALUES (?, ?, ?, ?, ?, ?, ?)";
	
		Connection conn = null;
		
		try {
			conn = dataSource.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, user.getfName());
			ps.setString(2, user.getlName());
			ps.setString(3, user.getBirthday());
			ps.setString(4, user.getInsuranceNumber());
			ps.setString(5, user.getSSN());
			ps.setString(6,user.getUsername());
			ps.setString(7, user.getPassword());
			ps.executeUpdate();
			ps.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	@Override
	public User getUserEmail(String email) {
		Query query = sessionFactory.getCurrentSession().createQuery("from User where email = :email");
		query.setParameter("email", email);
		query.setMaxResults(1);
		List<?> list = query.list();
		if(list.size() <= 0) {
			return null;
		}
		
		return (User)list.get(0);
	}
	
	@Override
	public User getUsername(String username) {
		Query query = sessionFactory.getCurrentSession().createQuery("from User where username = :username");
		query.setParameter("username", username);
		query.setMaxResults(1);
		List<?> list = query.list();
		if(list.size() <= 0) {
			return null;
		}
		
		return (User)list.get(0);
	}

}
